package com.ruoyi.store.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.store.domain.Payslip;
import com.ruoyi.store.domain.PayslipRate;
import com.ruoyi.store.mapper.PayslipMapper;
import com.ruoyi.store.service.IPayslipRateService;
import com.ruoyi.store.service.IPayslipService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 工资条Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-02
 */
@Service
public class PayslipServiceImpl implements IPayslipService {
    @Autowired
    private PayslipMapper payslipMapper;

    @Autowired
    private IPayslipRateService payslipRateService;

    /**
     * 查询工资条
     *
     * @param id 工资条主键
     * @return 工资条
     */
    @Override
    public Payslip selectPayslipById(Long id) {
        return payslipMapper.selectPayslipById(id);
    }

    /**
     * 查询工资条列表
     *
     * @param payslip 工资条
     * @return 工资条
     */
    @Override
    public List<Payslip> selectPayslipList(Payslip payslip) {
        return payslipMapper.selectPayslipList(payslip);
    }

    /**
     * 新增工资条
     *
     * @param payslip 工资条
     * @return 结果
     */
    @Override
    public int insertPayslip(Payslip payslip) {
        return payslipMapper.insertPayslip(payslip);
    }

    /**
     * 修改工资条
     *
     * @param payslip 工资条
     * @return 结果
     */
    @Override
    public int updatePayslip(Payslip payslip) {
        return payslipMapper.updatePayslip(payslip);
    }

    /**
     * 批量删除工资条
     *
     * @param ids 需要删除的工资条主键
     * @return 结果
     */
    @Override
    public int deletePayslipByIds(String ids) {
        return payslipMapper.deletePayslipByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工资条信息
     *
     * @param id 工资条主键
     * @return 结果
     */
    @Override
    public int deletePayslipById(Long id) {
        return payslipMapper.deletePayslipById(id);
    }

    @Override
    public void importExcel(Payslip payslip) throws Exception {
        PayslipRate rateParam = new PayslipRate();
        rateParam.setUserName(payslip.getUserName());
        List<PayslipRate> rateList = payslipRateService.selectPayslipRateList(rateParam);
        if (CollectionUtils.isEmpty(rateList)) {
            throw new Exception(payslip.getUserName() + "未设置提成比例");
        }
        if (rateList.size() != 1) {
            throw new Exception(payslip.getUserName() + "设置提成比例有重复，请检查确认");
        }
        PayslipRate rate = rateList.get(0);
        payslip.setCash(payslip.getCash().multiply(rate.getCashRate()));
        payslip.setRecharge(payslip.getRecharge().multiply(rate.getRechargeRate()));
        payslip.setWorkMore(payslip.getWorkMore().multiply(rate.getWorkMoreRate()));
        payslip.setMemberMore(payslip.getMemberMore().multiply(rate.getMemberMoreRate()));
        payslip.setMember(payslip.getMember().multiply(rate.getMemberRate()));
        payslip.setBasicSalary(rate.getBasicSalary());
        payslip.setReward(calReword(payslip.getTotalMoney()));
        payslip.setWages(payslip.getCash().add(payslip.getRecharge()).add(payslip.getWorkMore()).add(payslip.getMemberMore())
                .add(payslip.getMember()).add(payslip.getBasicSalary()).add(payslip.getReward()));
        Payslip payslipParam = new Payslip();
        payslipParam.setUserName(payslip.getUserName());
        payslipParam.setYear(payslip.getYear());
        payslipParam.setMonth(payslip.getMonth());
        List<Payslip> list = this.selectPayslipList(payslipParam);
        if (CollectionUtils.isNotEmpty(list)) {
            payslipMapper.deletePayslipById(list.get(0).getId());
        }
        payslipMapper.insertPayslip(payslip);
    }

    /**
     * 1万 800 1，2万 1000 1，6万 1200 2万1500
     *
     * @param totalMoney
     * @return
     */
    private BigDecimal calReword(BigDecimal totalMoney) {

        if (totalMoney.compareTo(new BigDecimal(10000)) >= 0 && totalMoney.compareTo(new BigDecimal(12000)) < 0) {
            return new BigDecimal(800);
        } else if (totalMoney.compareTo(new BigDecimal(12000)) >= 0 && totalMoney.compareTo(new BigDecimal(16000)) < 0) {
            return new BigDecimal(1000);
        } else if (totalMoney.compareTo(new BigDecimal(16000)) >= 0 && totalMoney.compareTo(new BigDecimal(20000)) < 0) {
            return new BigDecimal(1200);
        } else if (totalMoney.compareTo(new BigDecimal(20000)) >= 0 && totalMoney.compareTo(new BigDecimal(25000)) < 0) {
            return new BigDecimal(1500);
        } else if (totalMoney.compareTo(new BigDecimal(25000)) >= 0 && totalMoney.compareTo(new BigDecimal(30000)) < 0) {
            return new BigDecimal(2000);
        } else if (totalMoney.compareTo(new BigDecimal(30000)) >= 0) {
            return new BigDecimal(2500);
        } else {
            return BigDecimal.ZERO;
        }
    }
}
